package com.example.schoolexam2.controller;

import com.example.schoolexam2.entity.*;
import com.example.schoolexam2.enums.ResultEnum;
import com.example.schoolexam2.service.QuestionService;
import com.example.schoolexam2.vo.PageVo;
import com.example.schoolexam2.vo.QuestionsToListVo;
import com.example.schoolexam2.vo.ResultVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    QuestionService questionService;
    Logger logger = LoggerFactory.getLogger(getClass());

    //导入试题通过excle表的形式
    @PostMapping("/importQuestions")
    ResultVO<Exam> importQuestion(@RequestPart("file") MultipartFile file, HttpServletRequest request) throws Exception {
        //获取当前操作的老师的id
        String teacherId =(String) request.getAttribute("teacherId");
        logger.info("有请求访问importQuestions");
        if (file.isEmpty()) {
            System.out.println("文件未空");
            //没有上传文件
            return new ResultVO(ResultEnum.IMPORT_FAILED.getCode(), ResultEnum.IMPORT_FAILED.getMessage(), null);
        }
        int i = 0;
        //获取文件名判断当前文件是xls还是xlsx
        String fileName = file.getOriginalFilename().toLowerCase();
        if (fileName.endsWith(".xlsx")) {
            i = 1;
        } else if (fileName.endsWith(".xls")) {
            i = 2;
        } else {
            //不支持的文件类型
            return new ResultVO(ResultEnum.FAIL_ERROR.getCode(), ResultEnum.FAIL_ERROR.getMessage(), null);
        }
        boolean b = questionService.redAndSave(file, i,teacherId);
        if (b) {
            //保存成功
            return new ResultVO(ResultEnum.IMPORT_SUCCESS.getCode(), ResultEnum.IMPORT_SUCCESS.getMessage(), null);
        } else {
            //保存失败，文件有关键信息是空
            return new ResultVO(ResultEnum.IMPORT_FAILED.getCode(), ResultEnum.IMPORT_FAILED.getMessage(), null);
        }
    }

    //获取所有的试题
    @RequestMapping("/getAllQuestion")
    QuestionsToListVo getQuestion(String[] department,String isPublic) {
        logger.info("日志记录，调用getAllQuestions方法");
        System.out.println(isPublic==null);
        QuestionsToListVo allQuestion = questionService.getAllQuestion(department,isPublic);
        return allQuestion;
    }

    //删除选项
    @PostMapping("/delQuestion")
    public ResultVO delQuestion(@RequestBody String questionId, @RequestBody int type) {
        Boolean aBoolean = questionService.delQuestion(questionId, type);
        if (aBoolean) {
            //删除成功
            return new ResultVO(0, "删除成功", null);
        } else {
            return new ResultVO(-1, "删除失败", null);
        }
    }

    //查询选项
    @GetMapping("/gerQuestion")
    public ResultVO getQuestion(String questionId, String type) {
        Object obj = questionService.getQuestionById(questionId, type);
        if (obj instanceof QuestionRadio) {
            return new ResultVO(0, "获取成功", (QuestionRadio) obj);
        } else if (obj instanceof QuestionCheckd) {
            return new ResultVO(0, "获取成功", (QuestionCheckd) obj);
        } else if (obj instanceof QuestionFill) {
            return new ResultVO(0, "获取成功", (QuestionFill) obj);
        } else if (obj instanceof QuestionJudge) {
            return new ResultVO(0, "获取成功", (QuestionJudge) obj);
        } else {
             return new ResultVO(-1,"获取失败",null);
        }
    }
    //更新单选
    @PostMapping("/updateRadio")
    ResultVO updateQuestion(@RequestBody QuestionRadio radio){
        Boolean aBoolean = questionService.updateQuestion(radio);
        if (aBoolean){
             return new ResultVO(0,"更新成功",null);
        }
        return new ResultVO(-1,"更新失败",null);
    }
    @PostMapping("/updateCheckd")
    ResultVO updateQuestion(@RequestBody QuestionCheckd checkd){
        Boolean aBoolean = questionService.updateQuestion(checkd);
        if (aBoolean){
            return new ResultVO(0,"更新成功",null);
        }
        return new ResultVO(-1,"更新失败",null);
    }
    @PostMapping("/updateFill")
    ResultVO updateQuestion(@RequestBody QuestionFill fill){
        Boolean aBoolean = questionService.updateQuestion(fill);
        if (aBoolean){
            return new ResultVO(0,"更新成功",null);
        }
        return new ResultVO(-1,"更新失败",null);
    }
    @PostMapping("/updateJudge")
    ResultVO updateQuestion(@RequestBody QuestionJudge judge){
        Boolean aBoolean = questionService.updateQuestion(judge);
        if (aBoolean){
            return new ResultVO(0,"更新成功",null);
        }
        return new ResultVO(-1,"更新失败",null);
    }

    //分页获取题库
    @GetMapping("/getPageRadio")
    PageVo getRadio(String[] department, String isPublic,  Integer currentPage, Integer size,String qname){
        logger.info("请求分页radio{},{}",currentPage,size);
       return questionService.getPageRadio(department, isPublic, currentPage, size,qname);
    }
    @GetMapping("/getPageCheckd")
    PageVo getCheckd(String[] department,String isPublic,Integer currentPage,Integer size,String qname){
        logger.info("请求分页checkd{},{}",currentPage,size);
        return questionService.getPageCheckd(department,isPublic,currentPage,size,qname);
    }
    @GetMapping("/getPageFill")
    PageVo getFill(String[] department, String isPublic, Integer currentPage, Integer size,String qname){
        logger.info("请求分页fill{},{}",currentPage,size);
        return questionService.getPageFill(department,isPublic,currentPage,size,qname);
    }
    @GetMapping("/getPageJudge")
    PageVo getJudge(String[] department,String isPublic,Integer currentPage,Integer size,String qname){
        logger.info("请求分页judge{},{}",currentPage,size);
        return questionService.getPageJudge(department,isPublic,currentPage,size,qname);
    }

    @GetMapping("/getQuestionCount")
    int getQuestionCOunt(String type){
        return questionService.getQuestionCount(type);
    }
}
